/**
 * Created by zhaocong on 2016-6-24 10:41:43.
 */

var cc = window.cc || {};

cc.program = new function () {
  var self = this;

  /**
   * 模拟跳转
   */
  self.href = (function () {
    $('.href').tap(function () {
      window.location.href = $(this).data('href');
    });
  })();


  /**
   * 通过ID请求数据
   * @param id
   * @param url
   * @param callback
   * @private
   */
  self._request = function (id, url, callback) {
    $.ajax({
      type: 'get',
      url: url,
      data: id,
      dataType: 'json',
      success: function (data) {
        callback(data);
      },
      error: function () {
        layer.open({content: '链接超时'});
      }
    });
  };

  /**
   * 发票流水模板
   * @type {string}
   * @private
   */
  self._waterHtml = '<li class="ui-border-t">' +
      '<div class="rec-tit">' +
      '￥<p>__miane__</p>' +
      '</div>' +
      '<div class="ui-list-info">' +
      '<h4 class="ui-nowrap">优惠__yh__元   实际付款__fk__元</h4>' +
      '<p class="ui-nowrap">__time__</p>' +
      '</div>' +
      '</li>';

  /**
   * 打开关闭弹窗
   * @type {{open: cc.program.clayer.open, close: cc.program.clayer.close}}
   */
  self.clayer = {
    open: function () {
      $('.pop').show();
    },
    close: function () {
      $('.pop-close,.pop-mask').on('touchend', function (e) {
        e.preventDefault();
        $('.pop').hide();
      });
    }
  };


  /**
   * 发票消核
   * 显示发票明细，发票信息
   * @param url
   */
  self.invoiceMatching = function (url) {
    // 弹框
    $('.xh').tap(function () {
      self._request($(this).data('id'), url, seq);
    });

    self.clayer.close();

    /**
     * 序列化数据
     * @param data
     */
    function seq(data) {
      var _htmllist = [];
      $.each(data.details, function (i, el) {
        _htmllist.push(
            self._waterHtml
                .replace(/__miane__/, el.miane)
                .replace(/__yh__/, el.yh)
                .replace(/__fk__/, el.fk)
                .replace(/__time__/, el.time)
        );
      });

      $('#mx').html(_htmllist.join(''));
      $('#je').val(data.money);
      $('#tt').val(data.taitou);
      $('#ph').val(data.fapiaohao);
      $('#kpr').val(data.kaipiaoren);
      $('#fpxh').show();
    }
  };

  /**
   * 发票流水 readOnly
   * @param url
   */
  self.invoiceWater = function (url) {
    // 弹框
    $('.ls').tap(function () {
      self._request($(this).data('id'), url, seq);
    });

    self.clayer.close();

    function seq(data) {
      var _htmllist = [];
      $.each(data.details, function (i, el) {
        _htmllist.push(
            self._waterHtml
                .replace(/__miane__/, el.miane)
                .replace(/__yh__/, el.yh)
                .replace(/__fk__/, el.fk)
                .replace(/__time__/, el.time)
        );
      });

      $('#mx').html(_htmllist.join(''));
      $('#datatime').html(data.datatime);
      $('#tt').html(data.taitou);
      $('#kpje').html(data.money);
      $('#fpls').show();
    }
  };


};
